package in.muton.edushare.panels.folder;

import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.pages.folder.FolderHelper;

import java.util.HashMap;
import java.util.List;

import org.apache.wicket.markup.html.JavascriptPackageResource;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.dao.FileDirectoriesDAO;
import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.FileDirTypes;
import com.edushare.common.vo.FileDirectories;

public class CreateNewFolderPanel extends EdusharePanel {

	private static final long serialVersionUID = 5605050103157829063L;
	private IModel<FileDirectories> dirModel;
	public String selectedPrivacySetting;
	public String dirName;
	final DropDownChoice<String> privacyDDC;
	final TextField<String> dirNameTF;
	final Button createFolderBT;
	
	public CreateNewFolderPanel(String id, IModel<FileDirectories> dirModel, EduShareBasePage page) 
	{
		super(id, dirModel, page);
		this.dirModel = dirModel;
		add(JavascriptPackageResource.getHeaderContribution(CreateNewFolderPanel.class, "CreateNewFolderPanel.js"));
		Form<Void> newDirForm = new Form<Void>("newDirFormCNFP");
		newDirForm.setOutputMarkupId(true).setMarkupId("newDirFormCNFP");
		
		add(newDirForm);
		privacyDDC = 
											new DropDownChoice<String>
											("privacyDDCCNFP", 
											new PropertyModel<String>(this, "selectedPrivacySetting"),
											new PropertyModel<List<String>>(this, "folderPrivacyChoices"), 
											new PrivacyStringRenderer(getFolderPrivacyChoices()));
		newDirForm.add(privacyDDC);
		dirNameTF = new TextField<String>("dirNameCNFP", new PropertyModel<String>(this, "dirName"));
		dirNameTF.setOutputMarkupId(true).setMarkupId("dirNameCNFP");
		newDirForm.add(dirNameTF);
		
		createFolderBT = new Button("createDirButtonCNFP"){
			
			private static final long serialVersionUID = 8721000856548773250L;

			@Override
			public void onSubmit() {
				if(!StringUtil.isValid(dirNameTF.getInput())){
					setError("Folder name is required.");
					return;
				}
				
				//TODO: verify 1. if user is eligible to create directory, 2. directory of correct type
				
				//Create a new Directory
				FileDirectories fd = new FileDirectories();
				fd.setDirectoryName(CreateNewFolderPanel.this.dirName);
				fd.setReadPerm(CreateNewFolderPanel.this.selectedPrivacySetting);
				fd.setCreator(getLoggedInUser());
				new FileDirectoriesDAO(getORMInterface())
							.createNewDir(
									fd, 
									CreateNewFolderPanel.this.dirModel.getObject(), 
									FileDirTypes.USER_DIR);
				
				System.out.println("Name: "+ dirNameTF.getInput()+"\nPrivacy: "+privacyDDC.getInput() +"["+ privacyDDC.getConvertedInput()+"]" +
						"\nmodel: "+ selectedPrivacySetting);
			}
			
		};
		createFolderBT.setOutputMarkupId(true).setMarkupId("createDirButtonCNFP");
		newDirForm.add(createFolderBT);
		
		Label noSubDir = new Label("noSubDir", "");
		add(noSubDir);
		noSubDir.setVisible(false);
		
		//Block form and show warning if Upload not allowed.
		if(!FolderHelper.isEditable(this.dirModel.getObject(), getLoggedInUser())){
			newDirForm.setVisible(false);
			noSubDir.setDefaultModel(new Model<String>("Only the administrators and owners of this directory can create new sub-directory."));
			noSubDir.setVisible(true);
		}else if(this.dirModel.getObject().getDirType().getParentage()==FileDirTypes.PARENTAGE_FALSE){
			newDirForm.setVisible(false);
			noSubDir.setDefaultModel(new Model<String>("Subfolder creation is not valid in this directory."));
			noSubDir.setVisible(true);
		}else if(this.dirModel.getObject().getLevel()>=ApplicationConstants.MAX_DIR_DEPTH){
			newDirForm.setVisible(false);
			noSubDir.setDefaultModel(new Model<String>("No subdirectory can be created at this level."));
			noSubDir.setVisible(true);
		}
		
	}
	
	public List<String> getFolderPrivacyChoices(){
		List<String> choices =  FileDirectories.getValidFilePermissions(dirModel.getObject());
		if(choices!=null){
			if(choices.size()>1)
				this.selectedPrivacySetting = choices.get(1);
			else
				this.selectedPrivacySetting = choices.get(0);
		}
		return choices; 
	}
	
	public class PrivacyStringRenderer implements IChoiceRenderer<String>{

		private static final long serialVersionUID = 2117332567755642715L;
		private List<String> avlPerms;
		private HashMap<String, String> availablePermissionsMap;

		public PrivacyStringRenderer(List<String> avlPermissions){
			this.avlPerms = avlPermissions;
			this.availablePermissionsMap = 
					FolderHelper.getAvailablePermissionsMap(avlPerms, dirModel.getObject().getOwnerType());
		}
		
		public Object getDisplayValue(String object) {
			return availablePermissionsMap.get(object);
		}

		public String getIdValue(String object, int index) {
			return index+"";
		}
		
	}
}
